//
//  ISSettingViewController.m
//  iPhoneSafety
//
//  Created by dlon 2018/12/31.
//  Copyright © 2018年 com.li.ding. All rights reserved.
//

#import "ISSettingViewController.h"
#import "ISSettingCollectionViewCell.h"
#import "NWLazyProperty.h"
#import "ISCallSettingViewController.h"
#import "ISWordSettingViewController.h"
#import "ISPasswordViewController.h"
#import <libextobjc/EXTScope.h>
#import "UIColor+NWHex.h"
#import "UIFont+NWWeight.h"
#import "ISTakePhotoSettingViewController.h"

@interface ISSettingViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *list;

@end

@implementation ISSettingViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.title = @"设置";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    
    
    
    [self loadData];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
    self.navigationController.navigationBar.titleTextAttributes = @{
                                               NSForegroundColorAttributeName:[UIColor blackColor],
                                               NSFontAttributeName: [UIFont nw_systemFontOfSize:18 weight:NWFontWeightMedium]
                                               };
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.titleTextAttributes = @{ NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName: [UIFont nw_systemFontOfSize:18 weight:NWFontWeightMedium]};
}

- (void)loadData {
    [self.list removeAllObjects];
    
    //警报设置
    {
        NSMutableDictionary *dict = @{}.mutableCopy;
        dict[@"type"] = @"ISSettingCollectionViewCell";
        dict[@"height"] = @(70.f);
        dict[@"title"] = @"警报设置";
        dict[@"icon"] = @"img_setting_ring";
        @weakify(self)
        dispatch_block_t block = ^(void) {
            @strongify(self)
            ISCallSettingViewController *vc = [ISCallSettingViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        };
        dict[@"block"] = block;
        [self.list addObject:dict];
    }
    
    //密码设置
    {
        NSMutableDictionary *dict = @{}.mutableCopy;
        dict[@"type"] = @"ISSettingCollectionViewCell";
        dict[@"height"] = @(70.f);
        dict[@"title"] = @"密码设置";
        dict[@"icon"] = @"img_setting_password";
        @weakify(self)
        dispatch_block_t block = ^(void) {
            @strongify(self)
            ISPasswordViewController *vc = [ISPasswordViewController new];
            vc.type = [ISPassword pwd].length > 0 ? ISPasswordViewControllerTypeChg : ISPasswordViewControllerTypeNew;
            [self.navigationController pushViewController:vc animated:YES];
        };
        dict[@"block"] = block;
        [self.list addObject:dict];
    }
    
    //防盗抓拍
    {
        NSMutableDictionary *dict = @{}.mutableCopy;
        dict[@"type"] = @"ISSettingCollectionViewCell";
        dict[@"height"] = @(70.f);
        dict[@"title"] = @"防盗抓拍";
        dict[@"icon"] = @"img_setting_photo";
        @weakify(self)
        dispatch_block_t block = ^(void) {
            @strongify(self)
            ISTakePhotoSettingViewController *vc = [ISTakePhotoSettingViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        };
        dict[@"block"] = block;
        [self.list addObject:dict];
    }
    
    //防盗留言
    {
        NSMutableDictionary *dict = @{}.mutableCopy;
        dict[@"type"] = @"ISSettingCollectionViewCell";
        dict[@"height"] = @(70.f);
        dict[@"title"] = @"防盗留言";
        dict[@"icon"] = @"img_setting_word";
        @weakify(self)
        dispatch_block_t block = ^(void) {
            @strongify(self)
            ISWordSettingViewController *vc = [ISWordSettingViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        };
        dict[@"block"] = block;
        [self.list addObject:dict];
    }
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    if (@available(iOS 11.0, *)) {
        self.collectionView.frame = CGRectMake(0, self.view.safeAreaInsets.top, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) - self.view.safeAreaInsets.top);
    } else {
        self.collectionView.frame = CGRectMake(0, 64, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) - 64);
    }
    
}

#pragma mark - action
- (void)goBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UICollectionViewDelegate, UICollectionViewDataSource

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *viewModel = self.list[indexPath.row];
    dispatch_block_t block = viewModel[@"block"];
    if (block) {
        block();
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    NSDictionary *viewModel = self.list[indexPath.row];
    NSString *type = viewModel[@"type"];
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:type forIndexPath:indexPath];
    
    if ([type isEqualToString:@"ISSettingCollectionViewCell"]) {
        ISSettingCollectionViewCell *settingCell = (ISSettingCollectionViewCell *)cell;
        settingCell.iconView.image = [UIImage imageNamed:viewModel[@"icon"]];
        settingCell.titleLabel.text = viewModel[@"title"];
    }
    
    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.list.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *viewModel = self.list[indexPath.row];
    
    return CGSizeMake(CGRectGetWidth(collectionView.frame), [viewModel[@"height"] doubleValue]);
}

#pragma mark - set & get
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.sectionInset = UIEdgeInsetsZero;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        
        [_collectionView registerClass:[ISSettingCollectionViewCell class] forCellWithReuseIdentifier:@"ISSettingCollectionViewCell"];
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
        
        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _collectionView;
}

NWLazyPropertyWithInit(NSMutableArray, list, {
    
})

@end
